package com.sxhuayuan.parking.controller.agent;

import com.sxhuayuan.parking.compenent.Filter;
import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.Page;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.compenent.web.RespMessage.Status;
import com.sxhuayuan.parking.controller.BaseController;
import com.sxhuayuan.parking.entity.Agent;
import com.sxhuayuan.parking.entity.ParkingLot;
import com.sxhuayuan.parking.service.AgentRelationService;
import com.sxhuayuan.parking.service.AgentService;
import com.sxhuayuan.parking.service.ParkingLotService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController("agentParkingLotController")
@RequestMapping("/agent/parkinglot")
public class ParkingLotController extends BaseController {

	@Resource(name = "parkingLotServiceImpl")
	ParkingLotService parkingLotService;

	@Resource(name = "agentServiceImpl")
	AgentService agentService;

	@Resource(name = "agentRelationServiceImpl")
	AgentRelationService agentNexusService;

	/**
	 * 停车场分页条件查询
	 */
	@RequestMapping("/list")
	public RespMessage list(@RequestBody Pageable pageable) {

		Agent agent = agentService.getCurrent();
		if (agent == null) {
			return invalidToken();
		}

		List<Filter> filters = pageable.getFilters();
		Long childrenAgentId = null;
		if (filters != null) {
			for (Filter filter : filters) {
				if ("childrenAgentId".equals(filter.getProperty())) {
					childrenAgentId = ((Integer) filter.getValue()).longValue();
					break;
				}
			}
		}

		if (childrenAgentId != null) {
			Agent one = agentService.findOne(childrenAgentId);
			if (one == null) {
				return RespMessage.error("代理不存在");
			} else if (agentService.isPermission(agent, one)) {
				return successResp(parkingLotService.findPage(pageable, one));
			} else {
				return RespMessage.error("无权查看此代理");
			}
		} else {
			Page<ParkingLot> page = parkingLotService.findPage(pageable, agent);
			return successResp(page);
		}

	}

	/**
	 * 根据ID查询
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public RespMessage queryId(Long id) {
		ParkingLot parkingLot = parkingLotService.findOne(id);
		return RespMessage.builder().success().setDatas(parkingLot).build();
	}

	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public RespMessage save(@RequestBody @Validated ParkingLot parkingLot) {
		parkingLotService.save(parkingLot, agentService.getCurrent());
		return RespMessage.builder().success().setDatas(null).build();
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public RespMessage update(@RequestBody @Validated ParkingLot parkingLot) {
		parkingLot.setModifyDate(new Date());
		parkingLotService.update(parkingLot);
		return RespMessage.builder().success().build();
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public RespMessage deleteCarFieldId(Long id) {
		if (Objects.isNull(id) || id <= 0) {
			return RespMessage.error(Status.VALID_ERR, Message.FORM_VALID_FAIL);
		}
		ParkingLot parkingLot = parkingLotService.findOne(id);
		if (!parkingLot.getAgent().equals(agentService.getCurrent())) {
			return RespMessage.noPermissionError();
		}
		parkingLotService.delete(parkingLot);
		return RespMessage.builder().success().build();
	}

	/**
	 * 获取所有车场(包含子代理车场)
	 */
	@RequestMapping("/select")
	public RespMessage select() {
		return RespMessage.builder().success().setDatas(parkingLotService.findList(agentService.getCurrent())).build();
	}

}
